# coding=utf-8
import arcpy
path = r'D:\Test';
arcpy.env.workspace = path
shp = r'D:\Test\YA_RIV_LEVEL6_4543_1.shp'.decode('utf-8')
cs = arcpy.da.UpdateCursor(shp,['SHAPE@','name','level','flowRiv','flowId'])

shp2 = r'D:\Test\YA_RIV_LEVEL6_4543_2.shp'.decode('utf-8')
cs2 = arcpy.da.SearchCursor(shp2,['SHAPE@','name','level','rivId'])
i = 0
for row in cs:
    riverLastPoint = row[0].lastPoint
    riverLevel = row[2]
    riverName = row[1]
    disList = []
    for row2 in cs2:
        if row2[2] < riverLevel:
            distans = row2[0].distanceTo(riverLastPoint)
            nearestRivName = row2[1]
            res = [distans, nearestRivName,row2[3]]
            disList.append(res)
    cs2.reset()
    if (len(disList) > 0 and disList[0][0] < 100):
        disList.sort(key=lambda x: x[0])
        if(disList[0][0] != 0.0):
            print riverName,disList[0][0],"最近河流%s"%disList[0][1].encode('utf-8'),disList[0][2],'***********'
        else:
            print i
    i+=1
    # if(len(disList)>0):
    #     disList.sort(key=lambda x: x[0])
    #     print i,row[1],disList[0][1]
    #     i+=1
    #     row[3] = disList[0][1]
    #     row[4] = disList[0][2]
    #     row[5] = disList[0][0]
    #     cs.updateRow(row)
